package com.medical.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.medical.HttpTools.DataAnalyze;
import com.medical.HttpTools.HttpRequest;
import com.medical.po.Hospitalunitprice;
import com.medical.po.HospitalunitpriceCustom;
import com.medical.service.HospitalunitpriceService;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年8月21日 上午9:52:13 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/unitpriceMan")
public class UnitPriceController {
	/**
	 * 初始化单价设置界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/unitpriceSet")
	public ModelAndView unitpriceSet()throws Exception {
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/UnitPriceMan/UnitPriceSet");
		return model;
	}
	/**
	 *  单价设置提交类，插入到表hospitalunitprice
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="unitpriceSetSubmit")
	public @ResponseBody Map<String,Object> unitpriceSetSubmit(HttpServletRequest request)throws Exception {
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		
		String unitpricename = request.getParameter("unitpricename");
		String unitpricenumber = request.getParameter("unitpricenumber");
		String unitpriceabs = request.getParameter("unitpriceabs");
		String unitpriceoperatetime = request.getParameter("unitpriceoperatetime");
		String param = "unitpricename="+unitpricename+"&unitpricenumber="+unitpricenumber+"&unitpriceabs="+unitpriceabs+"&unitpriceoperatetime="+unitpriceoperatetime+"&hosploginid="+hosploginid;
		
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			JSONObject json = HttpRequest.sendPost("unitpriceset", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg = json.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg",jsonmsg);
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输错误，套餐设置失败！");
		}
		return result;
	}
	/**
	 * 初始化单价列表界面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/unitpriceList")
	public ModelAndView unitpriceList(HttpServletRequest request)throws Exception{
		/*HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		String param="hosploginid="+hosploginid;*/
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/UnitPriceMan/UnitPriceList");
		
		/*List<Map<String,Object>> list = new ArrayList();
		try {
			JSONObject json = HttpRequest.sendPost("getunitpricelist", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg = json.get("msg").toString();
			if("100".equals(jsoncode)) {
				try {
					JSONArray data = json.getJSONArray("data");
					for (int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<String,Object>();
						JSONObject job = data.getJSONObject(i);
						map.put("unitpriceid", job.get("unitpriceid").toString());
						map.put("unitpricename", job.get("unitpricename").toString());
						map.put("unitpricenumber", job.get("unitpricenumber").toString());
						map.put("unitpriceabs", job.get("unitpriceabs").toString());
						map.put("unitpriceoperatetime", job.get("unitpriceoperatetime").toString());
						list.add(map);
					}
				} catch (Exception e) {
					System.out.println("出错了，出错信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			System.out.println("出错了，出错信息是："+e.getMessage());
		}
		model.addObject("unitpriceList",list);*/
		return model;
	}
	/**
	 * 单价列表的ajax加载
	 * @param limit
	 * @param offset
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/unitpriceListAjax")
	public @ResponseBody Map<String,Object> unitpriceListAjax(Integer limit,Integer offset,HttpServletRequest request){
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		String param = "limit="+limit+"&offset="+offset+"&hosploginid="+hosploginid;//参数串
		
		String jsonobjectmsg = "";
		String jsonobjectcode ="";
		Integer total=0;
		List<Map<String,Object>> unitpricelist=new ArrayList<>();
		if(hosploginid!=null) {
			try {
				JSONObject Unitpricejson = HttpRequest.sendPost("getunitpricelist", param);
				jsonobjectmsg = Unitpricejson.get("msg").toString();
				jsonobjectcode = Unitpricejson.get("code").toString();
				try {
					if(Unitpricejson.get("data").toString()!="null") {
						JSONObject jsonobjectdata = Unitpricejson.getJSONObject("data");
						JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
						total=Integer.parseInt(jsonobjectdata.get("total").toString());
						unitpricelist = DataAnalyze.UnitpriceJsonToList(jsonarrayrows);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();  
		map.put("msg", jsonobjectmsg);
		map.put("rows", unitpricelist);
		map.put("code", jsonobjectcode);
        map.put("total", total);  
        
        System.out.println("输出这个map是："+map);
		return map;
	}
	
	/**
	 * 医院单价修改类，根据unitpriceid修改某条数据记录
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/unitpriceEditSubmit")
	public @ResponseBody Map<String,Object> unitpriceEditSubmit(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		
		String unitpriceid = request.getParameter("unitpriceid");
		String unitpricename = request.getParameter("unitpricename");
		String unitpricenumber = request.getParameter("unitpricenumber");
		String unitpriceabs = request.getParameter("unitpriceabs");
		String unitpriceoperatetime = request.getParameter("unitpriceoperatetime");
		String param = "unitpriceid="+unitpriceid+"&unitpricename="+unitpricename+"&unitpricenumber="+unitpricenumber+"&unitpriceabs="+unitpriceabs+"&unitpriceoperatetime="+unitpriceoperatetime+"&hosploginid="+hosploginid;
		System.out.println("输出这个参数param："+param);
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			JSONObject json = HttpRequest.sendPost("editunitprice", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg = json.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg", jsonmsg);
		} catch (Exception e) {
			System.out.println("出错了，输出错误信息："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输出错了，项目修改失败！");
		}
		return result;
	}
	/**
	 * 根据hosploginid和unitpriceid删除这条记录
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/DeleteUnitprice")
	public @ResponseBody Map<String,Object> DeleteUnitprice(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		Integer unitpriceid = Integer.parseInt(request.getParameter("unitpriceid"));
		
		String param = "hosploginid="+hosploginid+"&unitpriceid="+unitpriceid;
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			JSONObject json = HttpRequest.sendPost("deleteunitprice", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg = json.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg",jsonmsg);
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输错误，项目删除失败！");
		}
		return result;
	}
	
}
